<template>
	<view class="login-wrapper">
		<uni-forms ref="loginForm" labelPosition="top" :rules="loginRules" :modelValue="loginFormData">
			<uni-forms-item label="用户名" required name="username">
				<uni-easyinput v-model="loginFormData.username" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" required name="password">
				<uni-easyinput type="password" v-model="loginFormData.password" placeholder="请输入密码" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="login('loginForm')">登录</button>
		<button type="default" @click="switchEnv()">切换环境</button>
	</view>
</template>

<script>
	
	import * as accountService from '@/services/AccountService';
	import WebsocketTask from '@/utils/ws.js';
	import {setAuthorization,removeAuthorization,getAuthorization} from '@/utils/auth';
	import * as env from '@/env.js'

	export default {
		data() {
			return {
				loginFormData:{
					username:'jz_0001',
					password:'111111'
				},
				loginRules:{
					username: {
						rules: [{
							required: true,
							errorMessage: '用户名不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}]
					},
				}
			}
		},
		methods: {
			
			login(ref) {
				this.$refs[ref].validate().then(data => {
					console.log('success', data);
						
					accountService.loginByAc(data.username,data.password).then(res=>{
						console.log("jwt----,",res);
						let jwt = res.data;
						setAuthorization(jwt.token);
						
						
						uni.redirectTo({
							url:'/pages/main'
						})
					}).catch(err=>{
						removeAuthorization();
					});
					
					
				}).catch(err => {
					console.log('err', err);
				})
			},
			switchEnv(){
				uni.navigateTo({
					url:'/pages/setting/env'
				})
			}
		}
	}
</script>

<style>
	.login-wrapper{
		padding: 10px;
	}
</style>